package com.gexiaobao.pigeon.ui.fragment.mine.organization

import android.os.Bundle
import android.text.TextUtils
import androidx.fragment.app.setFragmentResult
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentOrgHistoryMatchBinding
import com.gexiaobao.pigeon.ui.activity.ActivityGameResult
import com.gexiaobao.pigeon.ui.adapter.OrgMatchAdapter
import com.gexiaobao.pigeon.viewmodel.OrgDetailViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/5/23 11:26
 * @Describe    : 往期赛事
 */
class FragmentHistoryMatch : BaseFragment<OrgDetailViewModel, FragmentOrgHistoryMatchBinding>() {

    private val mAdapter: OrgMatchAdapter by lazy { OrgMatchAdapter() }
    private lateinit var loadsir: LoadService<Any>

    private var mOrgId = ""

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose(getString(R.string.history_match)) {
            nav().navigateUp()
        }
        mOrgId = requireArguments().getString("orgId").toString()
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 5f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getOrgRaceListApp(false, mOrgId)
            }
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getOrgRaceListApp(true, mOrgId)
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getOrgRaceListApp(true, mOrgId)
        }

        mAdapter.addChildClickViewIds(R.id.go_race_pass)
        mAdapter.setOnItemChildClickListener { _, _, position ->
            if (mAdapter.data[position].racingPassId == 0 || TextUtils.isEmpty(mAdapter.data[position].name)) {
                RxToast.showToast(getString(R.string.There_are_no_ongoing_levels))
                return@setOnItemChildClickListener
            }
            startActivity<ActivityGameResult>(
                "title" to mAdapter.data[position].name,
                "passId" to mAdapter.data[position].racingPassId,
                "type" to "race"
            )
            activity?.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                val mRaceId = mAdapter.data[position].id
                val mRaceName = mAdapter.data[position].name
                setFragmentResult("rCHistoryRace", Bundle().apply {
                    putString("raceName", mRaceName)
                    putInt("raceId", mRaceId)
                    putString("type", "race")
                })
                nav().navigateUp()
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.getOrgRaceListApp(true, mOrgId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.orgMatchResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            } else {
                RxToast.showToast(it.errMessage)
            }
        }
    }
}